package com.weimob.mcloud.wechatwork.customer.mq.userPermission;

import com.weimob.artemis.client.consumer.model.ConsumerRecord;
import com.weimob.artemis.client.consumer.model.MessageResult;
import com.weimob.artemis.client.spring.annotation.ArtemisConsumerListener;
import com.weimob.artemis.client.spring.annotation.Subscribe;
import com.weimob.mcloud.wechatwork.customer.domain.ArtemisConstant;
import com.weimob.mcloud.wechatwork.customer.mq.artemisUtil.ArtemisExceptionListener;
import com.weimob.mcloud.wechatwork.customer.mq.userPermission.handler.UserPermissionChangeHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 用户数据隔离的权限更新消息消费者
 *
 * @Author LeoLee
 * @Date 2022/8/22 10:52
 * @Version 1.0
 */
@Component
@Slf4j
public class UserPermissionConsumer {

    @Autowired
    private UserPermissionChangeHandler userPermissionChange;

    @ArtemisExceptionListener("接收员工vid变更消息")
    @ArtemisConsumerListener(value = @Subscribe(topic = ArtemisConstant.Topic.WECHATWORK_PERMISSION_CHANGE_TOPIC, user = ArtemisConstant.PERMISSION_CHANGE_USER), interval = 50)
    public void userPermissionMsgHandler(MessageResult messagerResult) {
        //获取消息中实际业务数据
        List<ConsumerRecord> records = messagerResult.getRecords();
        //判断消息是否为空
        if (CollectionUtils.isEmpty(records)) {
            //如果消息的实际内容为空，直接跳过处理
            return;
        }
        //循环调用实际业务，处理消息
        for (ConsumerRecord record : records) {
            log.info("用户数据隔离权限变更消息, topic:{}, msg:{}, timestamp:{}", record.getTopic(), record.getValue(), record.getTimestamp());
            if (StringUtils.isNotEmpty(record.getValue())) {
                //实际处理数据隔离权限变更
                userPermissionChange.userPermissionChange(record.getValue());
            }
        }
    }
}
